package com.yc.spring.mvc.web;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyReqHandler implements HandlerInterceptor {

    @Override //请求到达目标资源之前执行的代码
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("------------- 请求到达目标资源之前执行的代码------------------");
        //允许访问目标资源 返回true
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override//请求从目标资源返回执行的代码
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("=================请求从目标资源返回执行的代码=====================");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("=================从页面返回执行的代码=======================");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
